package someTestExcemple.huaweiOd.logicAnalysis;
//华为OD机试 - 最长连续子序列 - 双指针（Java 2023 B卷 100分）
//https://blog.csdn.net/guorui_java/article/details/132261853
import java.util.*;
public class LongestContinuousSubsequence {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] nums = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        int target = Integer.parseInt(sc.nextLine());
        sc.close();
        System.out.println(getResult(nums,target));
        System.out.println(getResult_2(nums,target));

    }
    //用前缀和数组
    public static int getResult(int[] nums,int target) {
        int resultLen = -1;
        int n = nums.length;
        //前缀和数组 -要多一位
        int[] preSum = new int[n+1];
        for(int i=1;i<=nums.length;i++) {
            preSum[i] = preSum[i-1] +nums[i-1];
        }
        for(int l=0;l<=n;l++) {
            //可以看出第一个l=0的循环结束，如果没找到就一定没有
            for(int r=l+1;r<=n;r++) {
                System.out.println("r-l:"+ (r - l) + " "+"sum:"+ (preSum[r] - preSum[l]));
                if(preSum[r]-preSum[l] == target) {
                    resultLen = Math.max(resultLen,r-l);
                    break;
                }
            }
        }
        return resultLen;
    }

    //双指针
    public static int getResult_2(int[] nums,int target) {
        int resultLen = -1;
        int n = nums.length;
        int left = 0;
        int right =0;
        int tempSum = nums[left];
        while(right<n) {
            if(tempSum == target) {
                resultLen = Math.max(resultLen,right-left+1);
                tempSum -= nums[left];
                left++;
            } else if(tempSum < target) {
                right++;
                if(right== n) {
                    break;
                }
                tempSum+= nums[right];
            } else {
                tempSum-=nums[left];
                left++;
            }
        }
        return resultLen;
    }


}
